package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.constant.SeparatorConstant;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.device.util.StringUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.Device;
import com.wtwd.wtwddevice.model.entity.Upgrade;
import com.wtwd.wtwddevice.mq.PushMsgService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IUpgradeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 手表主动检测更新
 */
@MsgType(MsgTypeEnum.W_CHECK_UPDATE)
@Component
public class WCheckUpdateHandler implements IMsgHandler {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IUpgradeService upgradeService;

    @Resource
    private PushMsgService msgService;

    @Value("${upgrade.file}")
    private String upgradeFile;
    @Value("${file.http}")
    private String fileHttp;



    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        String body = wtProtocol.getBody();
        String[] split = body.split(SeparatorConstant.POUND_SIGN);
        String versionName = split[0];
        String version = split[1];
        Device device = deviceService.getDeviceInfoByImei(wtProtocol.getImei());
        String dataBody = "0";
        if (device != null && StringUtils.isEmpty(device.getWatchId())) {
            List<Upgrade> upgrades = upgradeService.getDeviceUpgradeListByWatchId(device.getWatchId());
            if (upgrades!=null&&upgrades.size()>0){
                Upgrade upgrade = upgrades.stream().max(Comparator.comparing(Upgrade::getVersion)).get();
                if (upgrade.getVersion().compareTo(version) > 0) {//有最新版本需要更新
                    dataBody=upgrade.getName()+SeparatorConstant.POUND_SIGN//名称
                            +version+SeparatorConstant.POUND_SIGN//版本号
                            +upgrade.getFileSize()+SeparatorConstant.POUND_SIGN//软件包大小
                            +(fileHttp.concat(upgradeFile).concat(upgrade.getFilePath()))+SeparatorConstant.POUND_SIGN//软件包路径
                            +upgrade.getResourcePackageSize()+SeparatorConstant.POUND_SIGN//资源包大小
                            +(fileHttp.concat(upgradeFile).concat(upgrade.getResourcePackagePath()));
                }
            }
        }
        wtProtocol.setTime(DateUtils.format(new Date(), DateUtils.dateFormat15));
        wtProtocol.setBody(dataBody);
        wtProtocol.setBodyLength(wtProtocol.getBody().length());
        wtProtocol.setMesType(CmdTypeEnum.REPORT_RESPONSE.getCmdType().toString());
        msgService.pushToDevice(messageBase.getForm(),
                MessageBase.builder().deviceKey(wtProtocol.getImei())
                        .wtProtocol(wtProtocol)
                        .build());

    }

    /**
     * @param str
     * @param flag
     * @return
     */
    public static String subString(String str, String flag) {
        String substring1 = str.substring(str.indexOf(flag) + 1);//去掉第一个元素
        String substring2 = substring1.substring(substring1.indexOf(flag) + 1);//去掉第二个元素;
        return substring2;
    }

    public static void main(String[] args) {
        System.out.println("1.1.6-Beta#116".length());
    }
}
